home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1999 March / EnigmA AMIGA RUN 35 (1999)(G.R. Edizioni)(IT)[!][issue 1999-03].iso / earcd / misc / pdflib / afmparse.h < prev    next >
C/C++ Source or Header  |  1999-01-01  |  11KB  |  308 lines

  1. /*
  2.  * Copyright (C) 1988, 1989, 1991 by Adobe Systems Incorporated. 
  3.  * All rights reserved.
  4.  */
  5.  
  6. /* ParseAFM.h
  7.  *
  8.  * This header file is used in conjuction with the parseAFM.c file.
  9.  * Together these files provide the functionality to parse Adobe Font
  10.  * Metrics files and store the information in predefined data structures.
  11.  * It is intended to work with an application program that needs font metric
  12.  * information. The program can be used as is by making a procedure call to 
  13.  * parse an AFM file and have the data stored, or an application developer
  14.  * may wish to customize the code. 
  15.  *
  16.  * This header file defines the data structures used as well as the key 
  17.  * strings that are currently recognized by this version of the AFM parser.
  18.  * This program is based on the document "Adobe Font Metrics Files, 
  19.  * Specification Version 2.0".
  20.  *
  21.  * AFM files are separated into distinct sections of different data. Because
  22.  * of this, the parseAFM program can parse a specified file to only save
  23.  * certain sections of information based on the application's needs. A record 
  24.  * containing the requested information will be returned to the application.
  25.  * 
  26.  * AFM files are divided into five sections of data:
  27.  *    1) The Global Font Information
  28.  *    2) The Character Metrics Information 
  29.  *    3) The Track Kerning Data
  30.  *    4) The Pair-Wise Kerning Data
  31.  *    5) The Composite Character Data
  32.  *
  33.  * Basically, the application can request any of these sections independent
  34.  * of what other sections are requested. In addition, in recognizing that
  35.  * many applications will want ONLY the x-width of characters and not all
  36.  * of the other character metrics information, there is a way to receive
  37.  * only the width information so as not to pay the storage cost for the 
  38.  * unwanted data. An application should never request both the 
  39.  * "quick and dirty" char metrics (widths only) and the Character Metrics 
  40.  * Information since the Character Metrics Information will contain all 
  41.  * of the character widths as well.
  42.  * 
  43.  * There is a procedure in parseAFM.c, called parseFile, that can be 
  44.  * called from any application wishing to get information from the AFM File.
  45.  * This procedure expects 3 parameters: a vaild file descriptor, a pointer
  46.  * to a (FontInfo *) variable (for which space will be allocated and then 
  47.  * will be filled in with the data requested), and a mask specifying
  48.  * which data from the AFM File should be saved in the FontInfo structure.
  49.  * 
  50.  * The flags that can be used to set the appropriate mask are defined below.
  51.  * In addition, several commonly used masks have already been defined. 
  52.  * 
  53.  * History:
  54.  *    original: DSM  Thu Oct 20 17:39:59 PDT 1988
  55.  *  modified: DSM  Mon Jul  3 14:17:50 PDT 1989
  56.  *    - added 'storageProblem' return code
  57.  *      - fixed typos
  58.  */
  59.  
  60. #include <stdio.h>
  61.  
  62.  
  63.  
  64. /* your basic constants */
  65. #define TRUE 1
  66. #define FALSE 0
  67. #define EOL '\n'                /* end-of-line indicator */
  68. #define MAX_NAME 4096           /* max length for identifiers */
  69. #define BooL int
  70. #define FLAGS int
  71.  
  72.  
  73.  
  74. /* Flags that can be AND'ed together to specify exactly what
  75.  * information from the AFM file should be saved.
  76.  */
  77. #define P_G    0x01    /* 0000 0001 */   /* Global Font Info      */
  78. #define P_W    0x02    /* 0000 0010 */   /* Character Widths ONLY */
  79. #define P_M    0x06    /* 0000 0110 */   /* All Char Metric Info  */
  80. #define P_P    0x08    /* 0000 1000 */   /* Pair Kerning Info     */
  81. #define P_T    0x10    /* 0001 0000 */   /* Track Kerning Info    */
  82. #define P_C    0x20    /* 0010 0000 */   /* Composite Char Info   */
  83.  
  84.  
  85. /* Commonly used flags
  86.  */
  87. #define P_GW    (P_G | P_W) 
  88. #define P_GM    (P_G | P_M)
  89. #define P_GMP    (P_G | P_M | P_P)
  90. #define P_GMK    (P_G | P_M | P_P | P_T) 
  91. #define PSC_ALL    (P_G | P_M | P_P | P_T | P_C)
  92.  
  93.  
  94.  
  95. /* Possible return codes from the parseFile procedure.
  96.  * 
  97.  * ok means there were no problems parsing the file.
  98.  *
  99.  * parseError means that there was some kind of parsing error, but the
  100.  * parser went on. This could include problems like the count for any given
  101.  * section does not add up to how many entries there actually were, or
  102.  * there was a key that was not recognized. The return record may contain
  103.  * vaild data or it may not. 
  104.  *
  105.  * earlyEOF means that an End of File was encountered before expected. This
  106.  * may mean that the AFM file had been truncated, or improperly formed.
  107.  * 
  108.  * storageProblem means that there were problems allocating storage for
  109.  * the data structures that would have contained the AFM data.
  110.  */
  111. #define ok 0
  112. #define parseError -1
  113. #define earlyEOF -2
  114. #define storageProblem -3
  115.  
  116.  
  117.  
  118. /************************* TYPES *********************************/
  119. /* Below are all of the data structure definitions. These structures
  120.  * try to map as closely as possible to grouping and naming of data 
  121.  * in the AFM Files.
  122.  */
  123.  
  124.  
  125. /* Bounding box definition. Used for the Font BBox as well as the 
  126.  * Character BBox.
  127.  */
  128. typedef struct
  129.    int llx;    /* lower left x-position  */
  130.    int lly;    /* lower left y-position  */
  131.    int urx;    /* upper right x-position */
  132.    int ury;    /* upper right y-position */
  133. } BBox;
  134.  
  135.  
  136. /* Global Font information.
  137.  * The key that each field is associated with is in comments. For an 
  138.  * explanation about each key and its value please refer to the AFM
  139.  * documentation (full title & version given above). 
  140.  */
  141. typedef struct
  142. {  
  143.    char *afmVersion;        /* key: StartFontMetrics */
  144.    char *fontName;        /* key: FontName */
  145.    char *fullName;        /* key: FullName */
  146.    char *familyName;        /* key: FamilyName */
  147.    char *weight;        /* key: Weight */
  148.    float italicAngle;        /* key: ItalicAngle */
  149.    BooL isFixedPitch;        /* key: IsFixedPitch */
  150.    BBox fontBBox;        /* key: FontBBox */
  151.    int underlinePosition;      /* key: UnderlinePosition */
  152.    int underlineThickness;     /* key: UnderlineThickness */
  153.    char *version;        /* key: Version */
  154.    char *notice;        /* key: Notice */
  155.    char *encodingScheme;    /* key: EncodingScheme */
  156.    int capHeight;        /* key: CapHeight */
  157.    int xHeight;            /* key: XHeight */
  158.    int ascender;        /* key: Ascender */
  159.    int descender;        /* key: Descender */
  160.    int StdVW;            /* key: StdVW */
  161.    int StdHW;            /* key: StdHW */
  162. } GlobalFontInfo;
  163.  
  164.  
  165. /* Ligature definition is a linked list since any character can have
  166.  * any number of ligatures.
  167.  */
  168. typedef struct _t_ligature
  169. {
  170.     char *succ, *lig;
  171.     struct _t_ligature *next;
  172. } Ligature;
  173.  
  174.  
  175. /* Character Metric Information. This structure is used only if ALL 
  176.  * character metric information is requested. If only the character
  177.  * widths is requested, then only an array of the character x-widths
  178.  * is returned.
  179.  *
  180.  * The key that each field is associated with is in comments. For an 
  181.  * explanation about each key and its value please refer to the 
  182.  * Character Metrics section of the AFM documentation (full title
  183.  * & version given above). 
  184.  */
  185. typedef struct
  186. {
  187.     int code,         /* key: C */
  188.         wx,        /* key: WX */
  189.         wy;        /* together wx and wy are associated with key: W */
  190.     char *name;     /* key: N */
  191.     BBox charBBox;    /* key: B */
  192.     Ligature *ligs;    /* key: L (linked list; not a fixed number of Ls */
  193. } CharMetricInfo;
  194.  
  195.  
  196. /* Track kerning data structure.
  197.  * The fields of this record are the five values associated with every 
  198.  * TrackKern entry.
  199.  *  
  200.  * For an explanation about each value please refer to the 
  201.  * Track Kerning section of the AFM documentation (full title
  202.  * & version given above). 
  203.  */
  204. typedef struct 
  205. {
  206.     int degree;  
  207.     float minPtSize, 
  208.           minKernAmt, 
  209.           maxPtSize, 
  210.           maxKernAmt;
  211. } TrackKernData;
  212.  
  213.  
  214. /* Pair Kerning data structure.
  215.  * The fields of this record are the four values associated with every
  216.  * KP entry. For KPX entries, the yamt will be zero.
  217.  *
  218.  * For an explanation about each value please refer to the 
  219.  * Pair Kerning section of the AFM documentation (full title
  220.  * & version given above). 
  221.  */
  222. typedef struct 
  223. {
  224.     char *name1;
  225.     char *name2;
  226.     int xamt,
  227.         yamt;
  228. } PairKernData;
  229.  
  230.  
  231. /* PCC is a piece of a composite character. This is a sub structure of a
  232.  * compCharData described below.
  233.  * These fields will be filled in with the values from the key PCC.
  234.  * 
  235.  * For an explanation about each key and its value please refer to the 
  236.  * Composite Character section of the AFM documentation (full title
  237.  * & version given above).  
  238.  */
  239. typedef struct
  240. {
  241.     char *pccName;
  242.     int deltax,
  243.         deltay;
  244. } Pcc;
  245.  
  246.  
  247. /* Composite Character Information data structure. 
  248.  * The fields ccName and numOfPieces are filled with the values associated
  249.  * with the key CC. The field pieces points to an array (size = numOfPieces)
  250.  * of information about each of the parts of the composite character. That
  251.  * array is filled in with the values from the key PCC.
  252.  * 
  253.  * For an explanation about each key and its value please refer to the 
  254.  * Composite Character section of the AFM documentation (full title
  255.  * & version given above).  
  256.  */
  257. typedef struct
  258. {
  259.     char *ccName;
  260.     int numOfPieces;
  261.     Pcc *pieces;
  262. } CompCharData;
  263.  
  264.  
  265. /*  FontInfo
  266.  *  Record type containing pointers to all of the other data
  267.  *  structures containing information about a font.
  268.  *  A a record of this type is filled with data by the
  269.  *  parseFile function.
  270.  */
  271. typedef struct FontInfo_s
  272.     GlobalFontInfo *gfi;    /* ptr to a GlobalFontInfo record */
  273.     int *cwi;            /* ptr to 256 element array of just char widths */ 
  274.     int numOfChars;        /* number of entries in char metrics array */
  275.     CharMetricInfo *cmi;    /* ptr to char metrics array */
  276.     int numOfTracks;        /* number to entries in track kerning array */
  277.     TrackKernData *tkd;        /* ptr to track kerning array */
  278.     int numOfPairs;        /* number to entries in pair kerning array */
  279.     PairKernData *pkd;        /* ptr to pair kerning array */
  280.     int numOfComps;        /* number to entries in comp char array */
  281.     CompCharData *ccd;        /* ptr to comp char array */
  282. } FontInfo;
  283.  
  284.  
  285.  
  286. /************************* PROCEDURES ****************************/
  287.  
  288. /*  Call this procedure to do the grunt work of parsing an AFM file.
  289.  *
  290.  *  "fp" should be a valid file pointer to an AFM file.
  291.  *
  292.  *  "fi" is a pointer to a pointer to a FontInfo record sturcture 
  293.  *  (defined above). Storage for the FontInfo structure will be
  294.  *  allocated in parseFile and the structure will be filled in
  295.  *  with the requested data from the AFM File.
  296.  *
  297.  *  "flags" is a mask with bits set representing what data should
  298.  *  be saved. Defined above are valid flags that can be used to set
  299.  *  the mask, as well as a few commonly used masks.
  300.  *
  301.  *  The possible return codes from parseFile are defined above.
  302.  */
  303.  
  304. int pdf_parseFile (FILE *fp, FontInfo **fi, FLAGS flags); 
  305. void freeStorage(FontInfo *fi);
  306.